{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def mccarthy91(n):\n",
    "    k = 1\n",
    "    while k:\n",
    "        if n > 100:\n",
    "            n -= 10\n",
    "            k -= 1\n",
    "        else:\n",
    "            n += 11\n",
    "            k += 1\n",
    "    return n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "don't forget to increase stack limit for recursive version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def mccarthy91_rec(n):\n",
    "    if n > 100:\n",
    "        return n - 10\n",
    "    else:\n",
    "        return mccarthy91_rec(mccarthy91_rec(n + 11))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "70 91\n",
      "71 91\n",
      "80 91\n",
      "81 91\n",
      "90 91\n",
      "91 91\n",
      "100 91\n",
      "101 91\n",
      "110 100\n",
      "111 101\n",
      "120 110\n",
      "121 111\n"
     ]
    }
   ],
   "source": [
    "for i in range(70, 130, 10):\n",
    "    print(i, mccarthy91(i))\n",
    "    print(i + 1, mccarthy91(i + 1))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
